package dut.ir.lxb.weibo.spider;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import dut.ir.lxb.weibo.bean.UserBean;
import dut.ir.lxb.weibo.bean.UserWeiboAction;
import dut.ir.lxb.weibo.bean.WeiboBean;
import dut.ir.lxb.weibo.db.UserInfoDao;
import dut.ir.lxb.weibo.util.ActionType;

public class ForwardDownloader
{
	protected WeiboDownloader wd;
	
	protected List<String> midList;
	
	protected List<String> userList;
	
	
	public ForwardDownloader()
	{
		wd = DownloadThread.loadWeiboDownloaderList().poll();
		wd.init();
		midList = loadMidList("./config/midlist.txt");
		userList = loadUserList("./config/userlist.txt");
	}
	
	public List<String> loadUserList(String userFile)
	{
		try
		{
			BufferedReader br = new BufferedReader(new FileReader(userFile));
			String line = null;
			List<String> userList = new ArrayList<String>();
			while((line = br.readLine()) != null)
			{
				if(!userList.contains(line))
					userList.add(line.trim());
				
			}
			
			br.close();
			
			return userList;
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
		
	}
	
	public List<String> loadMidList(String midFile)
	{
		List<String> midList = new ArrayList<String>();
		midList.add("3801428810100224");
		//midList.add("3801428810100224");
		return midList;
	}
	
	public void writeUsers(String userFile)
	{
		UserInfoDao userDao = UserInfoDao.getInstance();
		try
		{
			BufferedWriter bw = new BufferedWriter(new FileWriter(userFile));
			bw.write(UserBean.getHeader());
			bw.newLine();
			for(String user : userList)
			{
				UserBean ub = userDao.getUserByUid(user);
				if(ub == null || ub.getUid() == null || ub.getUid().trim().length() == 0)
				{
					ub = wd.downloadUserInfo(user);
					userDao.insertUser(ub);
				}
				if(ub == null || ub.getUid() == null || ub.getUid().trim().length() == 0 || ub.getNickName() == null || ub.getNickName().length() == 0)
					continue;
				bw.write(ub.toCSVFormat());
				bw.newLine();
			}
			bw.close();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AccountForbiddonException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	
	
	
//	public void downloadUsers()
//	{
//		wd.init();
//		wd.setAccessDataBase(true);
//		UserInfoDao userDao = UserInfoDao.getInstance();
//		if(!wd.checkAccount())
//		{
//			System.out.println("账号异常，退出下载");
//			return;
//		}
//		for(String user : userList)
//		{
//			WeiboBean wb = new WeiboBean();
//			wb.setMid(mid);
//			try
//			{
//				List<UserWeiboAction> actionList = wd.downloadUserWeiboAction(wb, ActionType.FORWARD);
//				for(UserWeiboAction action : actionList)
//				{
//					UserBean ub = wd.downloadUserInfo(action.getUid());
//					userDao.insertUser(ub);
//					System.out.println(String.format("正在下载用户[%s,%s]的个人信息", ub.getUid(), ub.getNickName()));
//					
//				}
//				Thread.currentThread();
//				Thread.sleep(500);
//				
//			} 
//			catch (AccountForbiddonException e)
//			{
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (InterruptedException e)
//			{
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			
//			
//			
//		}
//		
//	}
	
	
	public void download()
	{
		wd.init();
		wd.setAccessDataBase(true);
		UserInfoDao userDao = UserInfoDao.getInstance();
		if(!wd.checkAccount())
		{
			System.out.println("账号异常，退出下载");
			return;
		}
		for(String mid : midList)
		{
			WeiboBean wb = new WeiboBean();
			wb.setMid(mid);
			try
			{
				List<UserWeiboAction> actionList = wd.downloadUserWeiboAction(wb, ActionType.FORWARD);
				for(UserWeiboAction action : actionList)
				{
					UserBean ub = wd.downloadUserInfo(action.getUid());
					userDao.insertUser(ub);
					System.out.println(String.format("正在下载用户[%s,%s]的个人信息", ub.getUid(), ub.getNickName()));
					
				}
				Thread.currentThread();
				Thread.sleep(500);
				
			} 
			catch (AccountForbiddonException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
		}
		
	}
	
	public static void main(String [] args)
	{
		ForwardDownloader fd = new ForwardDownloader();
		
		//fd.writeUsers("./config/userinfo.csv");
		fd.download();
	}
	
	
	
}
